﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ContentRenderingApi;
using System.Reflection;

namespace SmoLiteApi.Sql
{
    /// <summary/>
    internal abstract class AbstractSql
    {
        private List<object> _objects;

        /// <summary/>
        internal AbstractSql()
        {
            this._objects = new List<object>();
        }

        /// <summary/>
        internal void AddObject(object value)
        {
            this._objects.Add(value);
        }

        /// <summary/>
        protected abstract string TemplateTypeName { get; }

        /// <summary/>
        protected abstract void ConfigureTemplate(AbstractTemplate template);

        /// <summary/>
        internal string Render()
        {
            AbstractTemplate template = new EmbeddedResourceTemplate(
                Assembly.GetExecutingAssembly(),
                Constant.TEMPLATE_NAME_SPACE,
                this.TemplateTypeName);

            // override the default characters 
            template.SpecialCharacterStart = Constant.CHAR_BRACKET_LEFT;
            template.SpecialCharacterEnd = Constant.CHAR_BRACKET_RIGHT;

            // override the default separators
            template.PreprocessingSeparatorImportStart = Constant.STRING_SEPARATOR_START;
            template.PreprocessingSeparatorImportEnd = Constant.STRING_SEPARATOR_END;

            // set the objects
            template.AddParameter(this._objects);

            // allow the sub class to configure the template
            this.ConfigureTemplate(template);
        
            // render
            return template.Render();
        }
    }
}
